﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyOrganizerModel.Database;

namespace MyOrganizerModel
{
    internal sealed class DataModel
    {
        public static DataModel GetInstance { get { return Instance(); } }
        private static DataModel DataModelInstance = null;
        private static DataModel Instance()
        {
            if (null == DataModelInstance)
                DataModelInstance = new DataModel();
            return DataModelInstance;
        }

        OrganizerEntities DbOrganizer = null;
        private DataModel()
        {
            if (DbOrganizer == null)
            {
                DbOrganizer = new OrganizerEntities();
            }
        }

        #region Books

        internal List<Books> GetBooks()
        {
            return DbOrganizer.Books.ToList();
        }
        internal List<Books> GetBooksByUserId(int UserId)
        {
            return DbOrganizer.Books.Where(b => b.UserId == UserId).ToList();
        }
        internal Books GetBookByBookId(int BookId)
        {
            return DbOrganizer.Books.Where(b => b.BookId == BookId).Single();
        }
        internal void DeleteBook(int BookId)
        {
            DbOrganizer.Books.DeleteObject(DbOrganizer.Books.Where(b => b.BookId == BookId).Single());
            DbOrganizer.SaveChanges();
        }
        internal void AddBook(Books Book)
        {
            DbOrganizer.Books.AddObject(Book);
            DbOrganizer.SaveChanges();
        }
        internal void UpdateBook(Books Book)
        {
            DbOrganizer.Books.ApplyCurrentValues(Book);
            DbOrganizer.SaveChanges();
        }

        #endregion

        #region Movies

        internal List<Movies> GetMovies()
        {
            return DbOrganizer.Movies.ToList();
        }
        internal List<Movies> GetMoviesByUserId(int UserId)
        {
            return DbOrganizer.Movies.Where(m => m.UserId == UserId).ToList(); ;
        }
        internal Movies GetMovieByMovieId(int MovieId)
        {
            return DbOrganizer.Movies.Where(m => m.MovieId == MovieId).Single();
        }
        internal void DeleteMovie(int MovieId)
        {
            var Movie = DbOrganizer.Movies.Where(m => m.MovieId == MovieId).Single();
            DbOrganizer.Movies.DeleteObject(Movie);
            DbOrganizer.SaveChanges();
        }
        internal void AddMovie(Movies Movie)
        {
            DbOrganizer.Movies.AddObject(Movie);
            DbOrganizer.SaveChanges();
        }
        internal void UpdateMovie(Movies Movie)
        {
            DbOrganizer.Movies.ApplyCurrentValues(Movie);
            DbOrganizer.SaveChanges();
        }

        #endregion

        #region TvShows

        internal List<TvShows> GetTvShows()
        {
            return DbOrganizer.TvShows.ToList();
        }
        internal List<TvShows> GetTvShowsByUserId(int UserId)
        {
            return DbOrganizer.TvShows.Where(t => t.UserId == UserId).ToList();
        }
        internal TvShows GetTvShowsByTvShowId(int TvShowId)
        {
            return DbOrganizer.TvShows.Where(t => t.TvShowId == TvShowId).Single();
        }
        internal void DeleteTvShow(int TvShowId)
        {
            DbOrganizer.TvShows.DeleteObject(DbOrganizer.TvShows.Where(t => t.TvShowId == TvShowId).Single());
            DbOrganizer.SaveChanges();
        }
        internal void AddTvShow(TvShows TvShow)
        {
            DbOrganizer.TvShows.AddObject(TvShow);
            DbOrganizer.SaveChanges();
        }
        internal void UpdateTvShow(TvShows TvShow)
        {
            DbOrganizer.TvShows.ApplyCurrentValues(TvShow);
            DbOrganizer.SaveChanges();
        }

        #endregion

        #region Users

        internal int GetUser(string UserName, string Password)
        {
            int UserId = 0;
            var User = DbOrganizer.Users.Where(u => u.Name == UserName && u.Password == Password).Single();
            if (User != null)
            {
                UserId = User.UserId;
            }
            return UserId;
        }
        internal Users GetUserById(int UserId)
        {
            return DbOrganizer.Users.Where(u => u.UserId == UserId).Single();
        }
        internal void AddUser(Users User)
        {
            DbOrganizer.Users.AddObject(User);
            DbOrganizer.SaveChanges();
        }
        internal void UpdateUser(Users User)
        {
            DbOrganizer.Users.ApplyCurrentValues(User);
            DbOrganizer.SaveChanges();
        }
        internal void DeleteUser(Users User)
        {
            DbOrganizer.Users.DeleteObject(User);
            DbOrganizer.SaveChanges();
        }
        internal void AddReport(Reports Report)
        {
            DbOrganizer.Reports.AddObject(Report);
            DbOrganizer.SaveChanges();
        }
        internal IList<Reports> GetReportsByUserId(int UserId)
        {
            return DbOrganizer.Reports.Where(r => r.UserId == UserId).ToList();
        }
        
        #endregion

    }
}
